package com.ktmall.ktmalluijava.controller;

import com.ktmall.ktmalluijava.pojo.Brand;
import com.ktmall.ktmalluijava.service.BrandService;
import com.ktmall.ktmalluijava.utils.Page;
import com.ktmall.ktmalluijava.utils.ResMsg;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * @author ZhouShuang
 * @version 17
 * @Title
 * @date 2025/5/13 上午11:50
 */
@RestController
@RequestMapping("/brand")
public class BrandController {

    @Resource
    private BrandService brandService;

    @GetMapping("/list")
    public String select(String name, Integer pageNo, Integer pageSize) {
        pageNo = pageNo == null ? 1 : pageNo;
        pageSize = pageSize == null ? 5 : pageSize;
        Page<Brand> brandPage = brandService.getPage(name, pageNo, pageSize);
        return ResMsg.success("品牌列表加载成功", brandPage);
    }

    @PostMapping("/add")
    public String add(@RequestBody Brand brand) {
        int i = brandService.add(brand);
        if (i > 0) {
            return ResMsg.success("新增成功");
        } else {
            return ResMsg.error("新增失败");
        }
    }

    @GetMapping("/detail")
    public String detail(Integer id) {
        Brand brand;
        try {
            brand = brandService.detail(id);
        } catch (Exception e) {
            return ResMsg.error("品牌详情查询失败", e);
        }
        return ResMsg.success("品牌详情查询成功",brand);
    }

    @PostMapping("/update")
    public String update(@RequestBody Brand brand){
        int i = brandService.update(brand);
        if(i>0){
            return ResMsg.success("修改成功");
        }else{
            return ResMsg.error("修改失败");
        }
    }

    @PostMapping("/delete")
    public String delete(@RequestBody Integer... id){
        int i = brandService.delete(id);
        if(i>0){
            return ResMsg.success("删除成功");
        }else{
            return ResMsg.error("删除失败");
        }
    }

    @GetMapping("/all")
    public String getAll(){
        return ResMsg.success("品牌全部信息加载成功",brandService.getAll());
    }
}
